#! /usr/bin/python3
#-*- coding: utf-8 -*-

# Async Library - IO,HTTP,Redis 
import asyncio
import aiohttp
import uvloop
import aioredis

# System Library
import signal

# CLI Library http://click.pocoo.org/
# Fix the issue in PYTHON 3 encoding issue for Click Library
import click 
import locale, codecs,os 
if codecs.lookup(locale.getpreferredencoding()).name == 'ascii':
    os.environ['LANG'] = 'en_US.utf-8'

# Design Library 
from logs import logger
import background

# Async Loop Library - uvloop makes asyncio 2-4x faster.
try:
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass

###############################################################
def run():
    logger.info("Fetcher start ")

    concurrency = 5
    try:
        loop = asyncio.get_event_loop()
        semaphore = asyncio.Semaphore(concurrency)

        # loop.run_until_complete(init_fetcher(
        # 'http://www.baidu.com', semaphore))
    except KeyboardInterrupt:
        # Canceling pending tasks and stopping the loop
        pass
    finally:
        end_time = datetime.now()
        # loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()

@click.command()
@click.option('--logging', '-l', default="INFO", envvar="SERVER_LOGGING", help="Log level", show_default=True)
@click.option('--debug', '-d', envvar="SERVER_DEBUG", is_flag=True, help="Enable debugging", show_default=True)
@click.option('--address', '-a', default="127.0.0.1", envvar="SERVER_ADDRESS", help="Server address", show_default=True)
@click.option('--port', '-p', default=8000, envvar="SERVER_PORT", help="Server port", show_default=True)
def main_cli(**options):
    """Run an CLI event source server.""" 
    # logging.basicConfig(level=getattr(logging, options['logging'].upper()))
    loop = asyncio.get_event_loop()
    loop.set_debug(options['debug'])
    logger.info("System Startup ! ")

    # server = Server(options['address'], options['port'], loop=loop)
    # loop.run_until_complete(server.start())
    try:
        pass
        # loop.run_forever()
    except KeyboardInterrupt:
        loop.stop()
    finally:
        loop.close()

## applications ##
if __name__ == "__main__":
    main_cli()
